/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI;

using SCS.Web.UI.WebControls.Design;

namespace SCS.Web.UI.WebControls
{
	public class MenuMarker : IStateManager
    {
        #region Fields
        private bool isTrackingViewState;
		private StateBag viewState;

		private string imageUrl = string.Empty;
		private string imageOverUrl = string.Empty;
		private string imageSelectedUrl = string.Empty;
		private string imageSelectedOverUrl = string.Empty;
		private string imageDisabledUrl = string.Empty;

		private int imageWidth = -1;
		private int imageHeight = -1;
        #endregion

        public MenuMarker()
		{
		}

		public MenuMarker(string imageUrl)
		{
			this.imageUrl = imageUrl;
		}

		public MenuMarker(
			string imageUrl, 
			string imageOverUrl)
		{
			this.imageUrl = imageUrl;
			this.imageOverUrl = imageOverUrl;
		}

		public MenuMarker(
			string imageUrl, 
			string imageOverUrl, 
			string imageSelectedUrl, 
			string normalSelectedOverUrl)
		{
			this.imageUrl = imageUrl;
			this.imageOverUrl = imageOverUrl;
			this.imageSelectedUrl = imageSelectedUrl;
            this.imageSelectedOverUrl = normalSelectedOverUrl;
		}

		public MenuMarker(
			string imageUrl, 
			string imageOverUrl, 
			string imageSelectedUrl, 
			string normalSelectedOverUrl, 
			string normalDisabledUrl)
		{
			this.imageUrl = imageUrl;
			this.imageOverUrl = imageOverUrl;
			this.imageSelectedUrl = imageSelectedUrl;
            this.imageSelectedOverUrl = normalSelectedOverUrl;
            this.imageDisabledUrl = normalDisabledUrl;
        }

        #region Properties
        [Bindable(true), Category("Image URLs"), DefaultValue(""), Description("Image URL to use for sub menu's parent item normal state.")]
		[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[NotifyParentProperty(true)]
		public string Url
		{
			get { return this.imageUrl; }
			set { this.imageUrl = value; }
		}
		
		[Bindable(true), Category("Image URLs"), DefaultValue(""), Description("Image URL to use when sub menu's parent item is hovered over.")]
		[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[NotifyParentProperty(true)]
		public string OverUrl
		{
			get { return this.imageOverUrl; }
			set { this.imageOverUrl = value; }
		}

		[Bindable(true), Category("Image URLs"), DefaultValue(""), Description("Image URL to use for sub menu's parent item when currently selected.")]
		[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[NotifyParentProperty(true)]
		public string SelectedUrl
		{
			get { return this.imageSelectedUrl; }
			set { this.imageSelectedUrl = value; }
		}

		[Bindable(true), Category("Image URLs"), DefaultValue(""), Description("Image URL for use when sub menu's parent item is currently selected and being hovered over.")]
		[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[NotifyParentProperty(true)]
		public string SelectedOverUrl
		{
			get { return this.imageSelectedOverUrl; }
			set { this.imageSelectedOverUrl = value; }
		}
		
		[Bindable(true), Category("Image URLs"), DefaultValue(""), Description("Image URL for use when sub menu's parent item is disabled.")]
		[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[NotifyParentProperty(true)]
		public string DisabledUrl
		{
			get { return this.imageDisabledUrl; }
			set { this.imageDisabledUrl = value; }
		}

		[Bindable(true), Category("Image Layout"), DefaultValue(""), Description("Image height of the sub menu maker.")]
		[NotifyParentProperty(true)]
		public int Height
		{
			get { return this.imageHeight; }
			set { this.imageHeight = value; }
		}
		
		[Bindable(true), Category("Image Layout"), DefaultValue(""), Description("Image width of the sub menu maker.")]
		[NotifyParentProperty(true)]
		public int Width
		{
			get { return this.imageWidth; }
			set { this.imageWidth = value; }
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
		public string Name
		{
			get 
			{ 
				return "MenuMarker";
			}
		}

		protected StateBag ViewState 
		{
			get 
			{
				if (viewState == null) 
				{ 
					viewState = new StateBag(false);
					if (isTrackingViewState) ((IStateManager)viewState).TrackViewState();
				}
				return viewState;
			}
        }
        #endregion

        internal void SetDirty() 
		{
			if (viewState != null) 
			{
				ICollection Keys = viewState.Keys;
				foreach (string key in Keys) 
				{
					viewState.SetItemDirty(key, true);
				}
			}
		}		
		public override string ToString() 
		{
			return GetType().Name; 
		}

		#region IStatemanager implementation
		
		bool IStateManager.IsTrackingViewState 
		{
			get 
			{
				return isTrackingViewState;
			}
		}

		void IStateManager.LoadViewState(object savedState) 
		{
			if (savedState != null) 
			{
				((IStateManager)ViewState).LoadViewState(savedState);

				string className = (string)ViewState["ImageUrl"];
				
				if (className != null)
					this.imageUrl = className;

				className = (string)ViewState["ImageOverUrl"];
				
				if (className != null)
					this.imageOverUrl = className;

				className = (string)ViewState["ImageSelectedUrl"];
				
				if (className != null)
					this.imageSelectedUrl = className;

				className = (string)ViewState["ImageSelectedOverUrl"];
				
				if (className != null)
					this.imageSelectedOverUrl = className;

				className = (string)ViewState["ImageDisabledUrl"];
				
				if (className != null)
					this.imageSelectedOverUrl = className;
			}
		}

		object IStateManager.SaveViewState() 
		{  
			string currentClass = this.imageUrl;
			string initialClass = (string)ViewState["ImageUrl"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["ImageUrl"] = currentClass;
			}
		
			currentClass = this.imageOverUrl;
			initialClass = (string)ViewState["ImageOverUrl"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["ImageOverUrl"] = currentClass;
			}

			currentClass = this.imageSelectedUrl;
			initialClass = (string)ViewState["ImageSelectedUrl"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["ImageSelectedUrl"] = currentClass;
			}

			currentClass = this.imageSelectedOverUrl;
			initialClass = (string)ViewState["ImageSelectedOverUrl"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["ImageSelectedOverUrl"] = currentClass;
			}

			currentClass = this.imageDisabledUrl;
			initialClass = (string)ViewState["ImageDisabledUrl"];
			
			if (initialClass == null)
				initialClass = string.Empty;

			if (currentClass.Equals(initialClass) == false) 
			{
				ViewState["ImageDisabledUrl"] = currentClass;
			}

			object initialDimension = null;
			int currentDimension = -1;

			currentDimension = this.imageWidth;
			initialDimension = ViewState["ImageWidth"];

			if (initialDimension != null && currentDimension.Equals((int)initialDimension) == false) 
			{
				ViewState["ImageWidth"] = currentDimension;
			}

			currentDimension = this.imageHeight;
			initialDimension = ViewState["ImageHeight"];

			if (initialDimension != null && currentDimension.Equals((int)initialDimension) == false) 
			{
				ViewState["ImageHeight"] = currentDimension;
			}

			if (viewState != null) 
			{
				return ((IStateManager)viewState).SaveViewState();
			}
			return null;
		}

		void IStateManager.TrackViewState() 
		{
			if (this.imageUrl.Length > 0)
				ViewState["ImageUrl"] = this.imageUrl;
	
			if (this.imageOverUrl.Length > 0)
				ViewState["ImageOverUrl"] = this.imageOverUrl;
			
			if (this.imageSelectedUrl.Length > 0)
				ViewState["ImageSelectedUrl"] = this.imageSelectedUrl;

			if (this.imageSelectedOverUrl.Length > 0)
				ViewState["ImageSelectedOverUrl"] = this.imageSelectedOverUrl;
			
			if (this.imageDisabledUrl.Length > 0)
				ViewState["ImageDisabledUrl"] = this.imageDisabledUrl;

			if (this.imageHeight > -1)
				ViewState["ImageHeight"] = this.imageHeight;

			if (this.imageWidth > -1)
				ViewState["ImageWidth"] = this.imageWidth;

			isTrackingViewState = true;

			if (viewState != null) 
			{
				((IStateManager)viewState).TrackViewState();
			}
		}

		
		#endregion
	}
}
